package so.sao.code.service.web.api;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import so.sao.code.dto.response.TagStatusInfoDto;
import so.sao.code.service.infrastructure.enums.CodeMessage;
import so.sao.code.service.service.TagStatusInfoService;

@RestController
@RequestMapping("api/tagQueryListStatus")
public class TagQueryListStatusFeignController {

	@Autowired
	private TagStatusInfoService tagStatusInfoService;

	/**
	 * 客服工具-标签状态查询api feign接口
	 * 
	 * @param queryList
	 *            标签序号、friendcode或二维码url
	 * @return
	 */
	@PostMapping(value = "/queryTagStatusInfoFromCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<TagStatusInfoDto> queryTagStatusInfoFromCode(@RequestBody List<String> queryList) {

		return queryList.parallelStream().map(new Function<String, TagStatusInfoDto>() {

			@Override
			public TagStatusInfoDto apply(String query) {
				try {
					return tagStatusInfoService.getTagStatusInfoBy(query, true);
				} catch (Exception e) {
					return new TagStatusInfoDto(CodeMessage.InvalidRequest.getId(), query + "/" + e.getMessage());
				}
			}
		}).collect(Collectors.toList());

	}

	/**
	 * 辅料管理-标签状态查询api feign接口
	 * 
	 * @param queryList
	 *            标签序号、friendcode或二维码url
	 * @return
	 */
	@PostMapping(value = "/getTagBaseInfoFromCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseEntity<TagStatusInfoDto> getTagBaseInfoFromCode(@RequestParam("tagParam") String tagParam) {

		return new ResponseEntity<TagStatusInfoDto>(tagStatusInfoService.getTagBaseInfoBy(tagParam), HttpStatus.OK);

	}

}
